package com.coolgor.coldot.schedule.Friday;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class QtimgTask {

    @Value("${schedule.Friday.stock}")
    private boolean OnOff;

    private  String BaseURL = "http://qt.gtimg.cn/q=";
    private List<String> stockCodeList = new ArrayList<String>();

    QtimgTask(){
        stockCodeList.add("sh601012");
        stockCodeList.add("sh600372");
    }

//	@Scheduled(cron = "0/5 * * * * ? ") //测试用 每5秒执行一次
//	@Scheduled(cron = "0 0/1 * * * ? ") //测试用 每分钟执行一次
//	@Scheduled(cron = "0 0/1 9-12,14-15 * * ?") //正式用
    public void touch()  {
        if (OnOff) {
            System.out.println(" => " + new Date());
            String urlStr = BaseURL;
            for (String code : stockCodeList) {
                urlStr += code + ",";
            }
            if (urlStr.endsWith(",")) {
                urlStr = urlStr.substring(0, urlStr.length()-1);
            }
            System.out.println(urlStr);
            try {
                Document doc = Jsoup.connect(urlStr).get();
                System.out.println(doc.html());
                System.out.println(doc.title());
                Elements newsHeadlines = doc.select("#mp-itn b a");
                for (Element headline : newsHeadlines) {
                    System.out.println(headline.attr("title"));
                    System.out.println(headline.absUrl("href"));
                }
            } catch ( IOException ioe) {
                System.err.println(ioe.getStackTrace());
            }
        }
    }
}
